import requests
import os

# Define the URL of your Flask application
url = "http://127.0.0.1:5000/translation"

# 获取当前脚本的绝对路径
current_file_path = os.path.abspath(__file__)
# 获取当前脚本所在目录
current_directory = os.path.dirname(current_file_path)

# 获取父目录
parent_directory = os.path.dirname(current_directory)


# Define the path to the input PDF file
pdf_file_path = os.path.join(parent_directory, "tests", "test.pdf")

output_file_path= os.path.join(parent_directory, "tests","api_test_output.pdf")
print(pdf_file_path)
# Define the target language for translation
target_language = "中文"

# Make a POST request to the /translation endpoint with the file and form data
with open(pdf_file_path, 'rb') as input_file:
    response = requests.post(url, files={'input_file': input_file}, data={'target_language': target_language})

# Check if the request was successful
if response.status_code == 200:
    # Save the translated file
    with open(output_file_path, 'wb') as output_file:
        output_file.write(response.content)
    print(f"Translated PDF saved to {output_file_path}")
else:
    # Print the error message
    print(f"Error: {response.json().get('message')}")